package cn.iocoder.yudao.module.operation.service.log;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.operation.controller.admin.log.vo.*;
import cn.iocoder.yudao.module.operation.dal.dataobject.log.LogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.operation.dal.mysql.log.LogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;

/**
 * 操作日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LogServiceImpl implements LogService {

    @Resource
    private LogMapper logMapper;

    @Override
    public Long createLog(LogSaveReqVO createReqVO) {
        // 插入
        LogDO log = BeanUtils.toBean(createReqVO, LogDO.class);
        logMapper.insert(log);

        // 返回
        return log.getId();
    }

    @Override
    public void updateLog(LogSaveReqVO updateReqVO) {
        // 校验存在
        validateLogExists(updateReqVO.getId());
        // 更新
        LogDO updateObj = BeanUtils.toBean(updateReqVO, LogDO.class);
        logMapper.updateById(updateObj);
    }

    @Override
    public void deleteLog(Long id) {
        // 校验存在
        validateLogExists(id);
        // 删除
        logMapper.deleteById(id);
    }

    @Override
        public void deleteLogListByIds(List<Long> ids) {
        // 删除
        logMapper.deleteByIds(ids);
        }


    private void validateLogExists(Long id) {
        if (logMapper.selectById(id) == null) {
            throw exception(new ErrorCode(500,"数据不存在"));
        }
    }

    @Override
    public LogDO getLog(Long id) {
        return logMapper.selectById(id);
    }

    @Override
    public PageResult<LogDO> getLogPage(LogPageReqVO pageReqVO) {
        return logMapper.selectPage(pageReqVO);
    }

}